package models

import (
	"fmt"
	"go-admin-official/modules/database"
	"go-admin-official/modules/utils"
	"time"
)

type UsersModel struct {
	Base

	Id          int64  `json:"id"`
	Name        string `json:"name"`
	UserName    string `json:"username"`
	Credit      int64  `json:"credit"`
	Description string `json:"description"`
	Avatar      string `json:"avatar"`
	Email       string `json:"email"`
	Phone       string `json:"phone"`
	Password    string `json:"password"`
	Ip          string `json:"ip"`
	Uuid        string `json:"uuid"`
	Gender      int64  `json:"gender"`
	Level       int64  `json:"level"`
	Coins       int64  `json:"coins"`
	IsVip       int64  `json:"is_vip"`
	Amount      int64  `json:"amount"`
	AmountFen   string `json:"-"`
	LoginAt     string `json:"login_at"`
	CreatedAt   string `json:"created_at"`
	UpdatedAt   string `json:"updated_at"`
}

func Users() UsersModel {
	return UsersModel{Base: Base{Table: "users"}}
}

func (u UsersModel) Find(id interface{}) UsersModel {
	item, _ := database.Table(u.Table).Find(id)
	return u.MapToModel(item)
}

func (u UsersModel) Refresh() UsersModel {
	item, _ := database.Table(u.Table).Find(u.Id)
	return u.MapToModel(item)
}

func (u UsersModel) UpdateLoginTime() {
	_, _ = database.Table(u.Table).
		Where("id", "=", u.Id).
		Update(database.H{
			"login_at": time.Now().Format("2006-01-02 15:04:05"),
		})
}

func (u UsersModel) Check(key, value string) UsersModel {
	item, _ := database.Table(u.Table).Where(key, "=", value).First()
	return u.MapToModel(item)
}

func (u UsersModel) Empty() bool {
	return u.Id == int64(0)
}

func (u UsersModel) Exist(name, email string) bool {
	item, _ := database.Table(u.Table).
		WhereRaw("name = ? or email = ?", name, email).
		First()
	return item == nil
}

func (u UsersModel) Insert() (UsersModel, error) {
	u.Uuid = utils.Uuid(25)

	uid, err := database.Table(u.Table).
		Insert(database.H{
			"name":     u.Name,
			"username": u.UserName,
			"email":    u.Email,
			"uuid":     u.Uuid,
			"password": u.Password,
			"ip":       u.Ip,
		})

	u.Id = uid
	return u, err
}

func (u UsersModel) Update() (int64, error) {

	if u.Name == "" && u.Avatar == "" {
		return 0, nil
	}

	return database.Table(u.Table).
		Where("id", "=", u.Id).
		Update(database.H{
			"avatar": u.Avatar,
			"name":   u.Name,
		})
}

func (u UsersModel) MapToModel(m map[string]interface{}) UsersModel {

	u.Id, _ = m["id"].(int64)
	u.Name, _ = m["name"].(string)
	u.UserName, _ = m["username"].(string)
	u.Description, _ = m["description"].(string)
	u.Avatar, _ = m["avatar"].(string)
	u.Email, _ = m["email"].(string)
	u.Phone, _ = m["phone"].(string)
	u.Password, _ = m["password"].(string)
	u.Ip, _ = m["ip"].(string)
	u.Uuid, _ = m["uuid"].(string)
	u.Gender, _ = m["gender"].(int64)
	u.Level, _ = m["level"].(int64)
	u.Credit, _ = m["credit"].(int64)
	u.Coins, _ = m["coins"].(int64)
	u.IsVip, _ = m["is_vip"].(int64)
	u.Amount, _ = m["amount"].(int64)
	u.AmountFen = fmt.Sprintf("%.2f", float64(u.Amount)/100)
	u.LoginAt, _ = m["login_at"].(string)
	u.CreatedAt, _ = m["created_at"].(string)
	u.UpdatedAt, _ = m["updated_at"].(string)

	return u
}
